﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WellQuery.aspx.cs" Inherits="GroundWater.Basedata.WellQuery" %>

<%@ Register src="WellEditView.ascx" tagname="WellEditView" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script language="javascript" src="../js/jquery.js" type="text/javascript"></script>
    <script language="javascript" src="../js/common.js" type="text/javascript"></script>
        <link media="all" href="../js/resources/_Common/Common.css" type=text/css rel=stylesheet />
<link media="all" href="../js/resources/_Loadding/Loadding.css" type=text/css rel=stylesheet />
<link media="all" href="../js/resources/_Panel/Panel.css" type=text/css rel=stylesheet />
<link media="all" href="../js/resources/_Button/Button.css" type=text/css rel=stylesheet />
<link media="all" href="../js/resources/_TextBox/TextBox.css" type=text/css rel=stylesheet />
</head>
<body>
    <form id="form1" runat="server"><ajaxToolkit:ToolkitScriptManager runat="server" ID="sm"  /><%--<asp:ScriptManager ID="sm" runat="server" />--%>
    <asp:ObjectDataSource ID="odsWells" runat="server" SelectMethod="GetAll" 
        TypeName="GroundWater.BusinessActions.Well" DeleteMethod="DeleteById" 
        ondeleting="odsWells_Deleting" >
    <SelectParameters>
        <asp:Parameter Name="isStoredProc" Type="Boolean" DefaultValue="False" />
        <asp:Parameter Name="enableCache" Type="Boolean" DefaultValue="True" />
    </SelectParameters>
    <DeleteParameters>
        <asp:Parameter Name="id" Type="String" />
        <asp:Parameter Name="isStoredProc" Type="Boolean" DefaultValue="False" />
        <asp:Parameter Name="removeCache" Type="Boolean" DefaultValue="True" />
        <asp:Parameter Name="logical" Type="Boolean" DefaultValue="False" />
    </DeleteParameters>
    </asp:ObjectDataSource>
    <div style="width:96%;">
        <p style="font-weight:bold; font-family:Tahoma; margin-bottom:10px; "></p>
        <div class="grid">
            <div class="rounded">
                <div class="top-outer"><div class="top-inner"><div class="top">
                    <h2>水井信息 </h2>
                </div></div></div>
                <div class="mid-outer"><div class="mid-inner"><div class="mid">     
                
                    <!-- Content Goes Here! -->
                    <asp:UpdatePanel ID="updPanel" runat="server" UpdateMode="Conditional">
                        <ContentTemplate>
                            <asp:GridView  ID="gvWells" runat="server" DataSourceID="odsWells" OnRowDataBound="RowDataBound" SkinID="PagingGridView" DataKeyNames="Code">
                                <Columns>
                                    <asp:BoundField  HeaderText="水源井代码" DataField="Code" SortExpression="Code" HeaderStyle-CssClass="first" ItemStyle-CssClass="first" />
                                    <asp:BoundField  HeaderText="水源井名称" DataField="Name"   SortExpression="Name" />
                                    <asp:BoundField  HeaderText="经度" DataField="Longitude"   SortExpression="Longitude" />
                                    <asp:BoundField  HeaderText="纬度" DataField="Latitude"   SortExpression="Latitude" />
                                    <asp:BoundField  HeaderText="井深" DataField="Depth"   SortExpression="Depth" />
                                    <asp:BoundField  HeaderText="凿井日期" DataField="ShaftSinkingDate" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="false"  SortExpression="ShaftSinkingDate" />
                                    <asp:BoundField  HeaderText="设备卡号" DataField="SIMCard"   SortExpression="SIMCard" />
                                    <asp:BoundField  HeaderText="设备地址" DataField="BTUAddress"   SortExpression="BTUAddress" />
                                    <asp:BoundField  HeaderText="剖面" DataField="SectionPlane"   SortExpression="SectionPlane" />
                                    <asp:TemplateField>
                                        <ItemTemplate><a href="javascript:showEditView('gvWells','<%# Eval("Code") %>', <%# Container.DataItemIndex + 1 %>)">编辑</a></ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:CommandField ShowDeleteButton="true" DeleteText="删除" />
                                </Columns>
                            </asp:GridView>
                        </ContentTemplate>
                    </asp:UpdatePanel>                     
    
                </div></div></div>
            <div class="bottom-outer"><div class="bottom-inner">
            <div class="bottom"></div></div></div>                   
        </div>      
        </div>
    </div>
    
    <!-- 面板内容 -->                
<div id="bodyPanel">
<asp:Panel ID="PopupPanel" runat="server" CssClass="x-pne" style="display:none; z-index:9999;  width:460px;">
    <asp:Panel ID="DragPanel" runat="server" CssClass="corner-bg x-pne-lt x-pne-left">
    <div class="corner-bg x-pne-rt x-pne-right">
    <div class="corner-bg x-pne-t drag">
    <div class=x-pne-btn style="background-position: 0px 0px" onclick="$find('editWell').hide();" ></div>
    <div class="x-pne-caption font">编辑水井信息</div>
    </div>
    </div>
    </asp:Panel>
    
    <div>
    <div class="side-bg x-pne-l">
    <div class="side-bg x-pne-r">
    <div class="x-pne-c" style="WIDTH: 448px; HEIGHT: 214px">
    <div id="contentPanel"><!-- 控件实例 -->

    <div class=gbPanel>
        <uc1:WellEditView ID="WellEditView1" runat="server" />
    </div>

    <div class="gbPanel" style="margin-top:20px; text-align:center;">
    <table class="method-area1" style="margin:0 auto; text-align:left;">
    <tbody>
    <tr>
    <td style="padding-left:30px;">
        <div>
            <div class="btn-side btn-bg" style="background-position: 0% 0%"></div>
            <div class="btn-body btn-bg" style="background-position: 0px 168px; WIDTH: 60px"><asp:Button ID="btnOK" runat="server" Text="  保存  " CssClass="font" /></div>
            <div class="btn-side btn-bg" style="background-position: 0px 189px"></div>
        </div>
    </td>
    <td style="padding-left:30px;">
        <div>
            <div class="btn-side btn-bg" style="background-position: 0% 0%"></div>
            <div class="btn-body btn-bg" style="background-position: 0px 168px; WIDTH: 60px"><asp:Button ID="btnClose" runat="server" Text="  取消  " CssClass="font" /></div>
            <div class="btn-side btn-bg" style="background-position: 0px 189px"></div>
        </div>
    </td>
    </tr>

    </tbody>
    </table>
    </div>


    </div>
    </div>
    </div>
    </div>
    
    <div class="corner-bg x-pne-lb x-pne-left">
        <div class="corner-bg x-pne-rb x-pne-right">
            <div class="corner-bg x-pne-b"></div>
        </div>
    </div>
    </div>
</asp:Panel>
</div>
<!-- 面板内容结束 -->
    
    <asp:Button id="btnShowPopupHelper" runat="server" style="display:none" />
    <ajaxToolKit:ModalPopupExtender ID="mdlPopup" runat="server" 
    BehaviorID="editWell" 
    TargetControlID="btnShowPopupHelper" 
    PopupControlID="PopupPanel" 
    DropShadow="false"
    PopupDragHandleControlID="DragPanel"
    BackgroundCssClass="modalBackground"
    CancelControlID="btnClose" OkControlID="btnOK"
    OnOkScript="onOk()"
/>
    <%--CancelControlID="btnClose" --%>
<script type="text/javascript">
    //<![CDATA[
    var rowIndex = -1;
    var mb1;
    jQuery(document).ready(function() {
        //mb1 = $find('editWell');
        //alert(mb1);
});
// :__doPostBack('gvWells','Page$6')
    var pageCount = '<%= gvWells.PageCount %>';

    Sys.Application.add_load(applicationLoadHandler);
    //Sys.Application.remove_load(applicationLoadHandler);
    function applicationLoadHandler() {
        //mb1 = $find('editWell');
        //alert(mb1);
    }
    
    
    function showEditView(gv, wellCode, index) {
        var mb = $find('editWell');
        if (mb) {
            //mb.add_showing(setWell);
        }
        if (rowIndex) {
            rowIndex = index;
        }
        if (wellCode) {
            setWell(wellCode);
        }
        else {
            clearWellInfo();
        }
        mb.show();

        alert(rowIndex);
    }

    // $('tr:eq(n-1)')
    function pageLoad() {
        var mpe = $find('editWell');
        if (mpe) {
            //mpe.add_showing(setWell);
            mpe.add_hiding(validateWell);
        }
    }

    function getWell() {
        var well = {
            "WellCode": jQuery("#txtWellCode", "#tblWell").val(),
            "WellName": jQuery("#txtWellName", "#tblWell").val(),
            "Longitude": jQuery("#txtLongitude", "#tblWell").val(),
            "Latitude": jQuery("#txtLatitude", "#tblWell").val(),
            "Depth": jQuery("#txtDepth", "#tblWell").val(),
            "ShaftSinkingDate": jQuery("#txtShaftSinkingDate", "#tblWell").val(),
            "SIMCard": jQuery("#txtSIMCard", "#tblWell").val(),
            "BTUAddress": jQuery("#txtBTUAddress", "#tblWell").val(),
            "SectionPlane": jQuery("#txtSectionPlane", "#tblWell").val()
        }
        if (well.Depth.length == 0) well.Depth = "0";
        if (well.Latitude.length == 0) well.Latitude = "0";
        if (well.Longitude.length == 0) well.Longitude = "0";
        if (well.ShaftSinkingDate.length == 0) well.ShaftSinkingDate = "1900-01-01";
        return well;
    }

    function setWell(wellCode) {
        jQuery.post(
                    "WellHandler.ashx?op=fetch",
                    { "wellcode": wellCode },
                    function(data) {
                        var well = Sys.Serialization.JavaScriptSerializer.deserialize(data);
                        if (data) {
                            populateWellInfo(well);
                        }
                    },
                    "text");
    }


    function setTableRow(tr, well) {
        tr.cells[0].innerText = well.WellCode;
        tr.cells[1].innerText = well.WellName;
        tr.cells[2].innerText = well.Longitude;
        tr.cells[3].innerText = well.Latitude;
        tr.cells[4].innerText = well.Depth;
        tr.cells[5].innerText = well.ShaftSinkingDate;
        tr.cells[6].innerText = well.SIMCard;
        tr.cells[7].innerText = well.BTUAddress;
        tr.cells[8].innerText = well.SectionPlane;
    }
    
    function populateWellInfo(well) {
        jQuery("#txtWellCode", "#tblWell").val(well.WellCode);
        jQuery("#txtWellName", "#tblWell").val(well.WellName);
        jQuery("#txtLongitude", "#tblWell").val(well.Longitude);
        jQuery("#txtLatitude", "#tblWell").val(well.Latitude);
        jQuery("#txtDepth", "#tblWell").val(well.Depth);
        jQuery("#txtShaftSinkingDate", "#tblWell").val(well.ShaftSinkingDate.format("yyyy-MM-dd"));
        jQuery("#txtSIMCard", "#tblWell").val(well.SIMCard);
        jQuery("#txtBTUAddress", "#tblWell").val(well.BTUAddress);
        jQuery("#txtSectionPlane", "#tblWell").val(well.SectionPlane)
    }

    function clearWellInfo() {
        jQuery("#txtWellCode", "#tblWell").val("");
        jQuery("#txtWellName", "#tblWell").val("");
        jQuery("#txtLongitude", "#tblWell").val("");
        jQuery("#txtLatitude", "#tblWell").val("");
        jQuery("#txtDepth", "#tblWell").val("");
        jQuery("#txtShaftSinkingDate", "#tblWell").val("");
        jQuery("#txtSIMCard", "#tblWell").val("");
        jQuery("#txtBTUAddress", "#tblWell").val("");
        jQuery("#txtSectionPlane", "#tblWell").val("")
    }

    function onOk() {
//        var selector = "tr:eq(" + rowIndex + "-1)";
//        var selector = "tr:eq(7-1)";
//        var t = jQuery(selector, "#gvWells");
//        //alert(t);
//        var row = t[0];

        var tr = document.getElementById("gvWells").rows[rowIndex];

        var well = getWell();

        if (rowIndex > -1) {
            setTableRow(tr, well)
        }

        alert(rowIndex);

        jQuery.post(
                     rowIndex > -1 ? "WellHandler.ashx?op=update" : "WellHandler.ashx?op=insert",
                    { "well": Sys.Serialization.JavaScriptSerializer.serialize(well) },
                    function(data) {
                        //var well = Sys.Serialization.JavaScriptSerializer.deserialize(data);
                        if(document.getElementById("gvWells").rows.length < 12) return;

                        if (typeof (rowIndex) === "undefined" || rowIndex < 0) {
                            alert(pageCount );
                            var lastPage = 'Page$' + pageCount;
                            //__doPostBack('gvWells', 'Page$6');
                        }
                    },
                    "text");

                    
    }


    function validateWell(sender, eventArgs) {
        var well = getWell();
        var cancel = false;
        if (well.WellCode.length == 0) {
            cancel = true;
            alert("水井代号不能为空。");
        }
        if (well.WellName.length == 0) {
            cancel = true;
            alert("水井名称不能为空。");
        }
        if (!isNum(well.Depth)) {
            cancel = true;
            alert("井深为数值。");
        }
        if (!isNum(well.Longitude)) {
            cancel = true;
            alert("井的经度为数值。");
        }
        if (!isNum(well.Latitude)) {
            cancel = true;
            alert("井的纬度为数值。");
        }
        if (!validateCNDate(well.ShaftSinkingDate)) {
            cancel = true;
            alert("开凿日期格式不正确。");
        }
        if (cancel) {
            eventArgs.set_cancel(true);
        }
    }
    
//]]>
</script>
    
    
<div style="margin-left:10px; margin-top:10px;"><input id="btnAdd" name="btnAdd" type="button" value="添加水井" onclick="showEditView('gvWells','', -1);" /></div>

    </form>
</body>
</html>
